using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using Ruminate.GUI.Framework;
using Ruminate.Utils;

namespace Ruminate.GUI.Content {

    /// <summary>
    /// Aligns the current element by offsetting its top left from its parents top-left corner. 
    /// </summary>
    public class Offset : LocationComponent {

        #region Exposed Data

        public override sealed Vector2 Location { get; set; }
        public override sealed Vector2 Size { get; set; }

        override public Vector2 RenderLocation { get { return _renderLocation; } }
        override public Rectangle RenderBounds { get { return _renderBounds; } }

        override public Vector2 RenderSafeAreaLocation { get { return _renderSafeAreaLocation; } }
        override public Rectangle RenderSafeArea { get { return _safeArea; } }

        override public sealed Vector2 ChildOffset { get; set; }

        #endregion

        /// <summary>
        /// 
        /// </summary>
        private Vector2 _renderLocation, _renderSafeAreaLocation;
        private Rectangle _renderBounds, _safeArea;

        public Offset(int x, int y, int width, int height) {

            Location = new Vector2(x, y);
            Size = new Vector2(width, height);

            _renderLocation = Location;
            _renderSafeAreaLocation = Location;

            _renderBounds = new Rectangle(x, y, width, height);
            _safeArea = _renderBounds;

            ChildOffset = Vector2.Zero;
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="graphicsDevice"></param>
        internal protected override void AlignToViewport(GraphicsDevice graphicsDevice) {

            Location = Vector2.Zero;
            Size = new Vector2(graphicsDevice.Viewport.Width, graphicsDevice.Viewport.Height);

            _renderLocation = Location;
            _renderBounds = graphicsDevice.Viewport.Bounds;
            _renderSafeAreaLocation = Vector2.Zero;
            _safeArea = graphicsDevice.Viewport.Bounds;
        }

        /// <summary>
        /// 
        /// </summary>
        internal protected override void AlignToParentElement() {            

            _renderLocation =
                LinkedElement.Parent.LocationComponent.RenderSafeAreaLocation
                + -LinkedElement.Parent.LocationComponent.ChildOffset
                + Location;

            _renderBounds.X = (int)_renderLocation.X;
            _renderBounds.Y = (int)_renderLocation.Y;
            _renderBounds.Width = (int)Size.X;
            _renderBounds.Height = (int)Size.Y;

            var safeArea = LinkedElement.ChildRenderArea();

            _renderSafeAreaLocation = safeArea.Location.ToVector2() + _renderLocation;

            _safeArea.X = (int)_renderSafeAreaLocation.X;
            _safeArea.Y = (int)_renderSafeAreaLocation.Y;
            _safeArea.Width = safeArea.Width;
            _safeArea.Height = safeArea.Height;         

            _safeArea = Rectangle.Intersect(_safeArea, 
                LinkedElement.Parent.LocationComponent.RenderSafeArea);
        }        
    }
}
